; 小狼毫输入法安装程序 - Inno Setup 脚本
#define MyAppName "小狼毫"
#define MyAppVersion "0.17.4"
#define MyAppPublisher "式恕堂"
#define MyAppURL "https://rime.im/"
#define MyAppExeName "WeaselServer.exe"

[Setup]
; 基本信息
AppId=weasel_inno_setup
AppName={#MyAppName}
AppVersion={#MyAppVersion}
AppVerName={#MyAppName} {#MyAppVersion}
AppPublisher={#MyAppPublisher}
AppPublisherURL={#MyAppURL}
AppSupportURL={#MyAppURL}
AppUpdatesURL={#MyAppURL}
DefaultDirName={autopf64}\Rime\weasel-{#MyAppVersion}
DefaultGroupName={#MyAppName}
UninstallDisplayName={#MyAppName}
UninstallDisplayIcon={app}\WeaselServer.exe
OutputDir=.
OutputBaseFilename=weasel-{#MyAppVersion}-installer
Compression=lzma2/max
SolidCompression=yes
WizardStyle=modern

; 安装目录设置
UsePreviousAppDir=no
DirExistsWarning=auto
DisableDirPage=no
DisableProgramGroupPage=yes

; 强制所有用户安装
PrivilegesRequired=admin
PrivilegesRequiredOverridesAllowed=commandline

; 界面设置
SetupIconFile=weasel.ico
LicenseFile={#MyAppVersion}\LICENSE.txt

[Languages]
Name: "chinesesimp"; MessagesFile: "compiler:Default.isl,ChineseSimplified.isl"

[CustomMessages]
chinesesimp.DisplayName=小狼毫输入法
chinesesimp.LaunchWeasel=启动小狼毫
chinesesimp.StopWeasel=停止小狼毫
chinesesimp.Settings=输入法设置
chinesesimp.DictManager=用户词典管理
chinesesimp.Sync=用户资料同步
chinesesimp.Deploy=重新部署
chinesesimp.UserFolder=用户文件夹
chinesesimp.AppFolder=程序文件夹
chinesesimp.Update=检查新版本
chinesesimp.Setup=安装选项
chinesesimp.Manual=说明书
chinesesimp.Server=小狼毫算法服务
chinesesimp.Uninstall=卸载小狼毫
chinesesimp.InstallWubi86=极点五笔86

[Tasks]
; 安装五笔86方案的任务
Name: "installwubi"; Description: "{cm:InstallWubi86}"; GroupDescription: "安装额外的输入方案:"; Flags: unchecked

[Files]
; 主程序文件
Source: "0.17.4\*.*"; DestDir: "{app}"; Flags: ignoreversion recursesubdirs createallsubdirs; Excludes: uninstall.exe
Source: "setup.bat"; DestDir: "{app}"; Flags: ignoreversion

; 用户数据文件 - 根据任务选择安装
Source: "userdata\*.*"; DestDir: "{userappdata}\Rime"; Flags: ignoreversion recursesubdirs createallsubdirs;
Source: "rime-wubi86-jidian\*.*"; DestDir: "{userappdata}\Rime"; Flags: ignoreversion recursesubdirs createallsubdirs; Tasks: installwubi

[Icons]
; 开始菜单快捷方式
Name: "{group}\{cm:Server}"; Filename: "{app}\WeaselServer.exe"
Name: "{group}\{cm:UserFolder}"; Filename: "{app}\WeaselServer.exe"; Parameters: "/userdir"
Name: "{group}\{cm:DictManager}"; Filename: "{app}\WeaselDeployer.exe"; Parameters: "/dict"
Name: "{group}\{cm:Uninstall}"; Filename: "{uninstallexe}"

[Run]
; 安装完成后运行设置程序
Filename: "{app}\setup.bat"; Parameters: "/i"; StatusMsg: "正在注册输入法..."; Flags: runhidden waituntilterminated
Filename: "{app}\WeaselServer.exe"; Description: "{cm:LaunchWeasel}"; Flags: nowait skipifsilent


[UninstallRun]
; 卸载前停止服务
Filename: "{app}\WeaselServer.exe"; Parameters: "/quit"; RunOnceId: "StopWeasel"; Flags: runhidden waituntilterminated
Filename: "{app}\WeaselSetup.exe"; Parameters: "/u"; RunOnceId: "UnregisterWeasel"; Flags: runhidden waituntilterminated

[UninstallDelete]
; Delete other files on uninstall
Type: filesandordirs; Name: "{userappdata}\Rime"

[Registry]
; 注册安装信息
; Root: HKLM; Subkey: "SOFTWARE\Rime\Weasel"; ValueType: string; ValueName: "InstallDir"; ValueData: "{app}"; Flags: uninsdeletekey
; Root: HKLM; Subkey: "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Weasel"; ValueType: string; ValueName: "DisplayName"; ValueData: "{cm:DisplayName}"; Flags: uninsdeletekey
; Root: HKLM; Subkey: "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Weasel"; ValueType: string; ValueName: "UninstallString"; ValueData: """{uninstallexe}"""; Flags: uninsdeletekey
; Root: HKLM; Subkey: "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Weasel"; ValueType: string; ValueName: "DisplayVersion"; ValueData: "{#MyAppVersion}"; Flags: uninsdeletekey
; Root: HKLM; Subkey: "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Weasel"; ValueType: string; ValueName: "Publisher"; ValueData: "{#MyAppPublisher}"; Flags: uninsdeletekey
; Root: HKLM; Subkey: "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Weasel"; ValueType: string; ValueName: "URLInfoAbout"; ValueData: "{#MyAppURL}"; Flags: uninsdeletekey
; Root: HKLM; Subkey: "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Weasel"; ValueType: string; ValueName: "HelpLink"; ValueData: "https://rime.im/docs/"; Flags: uninsdeletekey
; Root: HKLM; Subkey: "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Weasel"; ValueType: dword; ValueName: "NoModify"; ValueData: "1"; Flags: uninsdeletekey
; Root: HKLM; Subkey: "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Weasel"; ValueType: dword; ValueName: "NoRepair"; ValueData: "1"; Flags: uninsdeletekey

; 开机启动项
Root: HKLM; Subkey: "SOFTWARE\Microsoft\Windows\CurrentVersion\Run"; ValueType: string; ValueName: "WeaselServer"; ValueData: """{app}\WeaselServer.exe"""; Flags: uninsdeletevalue

[Code]
// 检查操作系统版本和位数
function InitializeSetup(): Boolean;
var
  Version: TWindowsVersion;
  IsCompatible: Boolean;
  SystemInfo: String;
  BitStr: String;
begin
  // 获取 Windows 版本信息
  GetWindowsVersionEx(Version);

  // 检查是否为 Windows 10 或以上，且是 64 位系统
  IsCompatible := (Version.Major >= 10) and IsWin64;

  if not IsCompatible then
  begin
    // 确定系统位数描述
    if IsWin64 then
      BitStr := '64位'
    else
      BitStr := '32位';

    // 构建详细的系统信息字符串
    SystemInfo := '检测到您的系统信息：' + #13#10 +
                  'Windows 版本: ' + IntToStr(Version.Major) + '.' + IntToStr(Version.Minor) + #13#10 +
                  '系统位数: ' + BitStr + #13#10 + #13#10 +
                  '此版本的输入法要求 Windows 10 或以上版本的 64 位操作系统。' + #13#10 +
                  '继续安装可能无法正常工作。' + #13#10 + #13#10 +
                  '是否要继续安装？';

    // 显示警告对话框，用户选择"否"则取消安装
    if MsgBox(SystemInfo, mbError, MB_YESNO) = IDNO then
    begin
      Result := False; // 用户选择取消安装
      Exit;
    end;
    // 用户选择"是"则继续安装
  end;

  Result := True; // 系统兼容或用户坚持安装
end;
